package cn.elead.chaos.framework.util;

import javax.servlet.http.HttpServletRequest;

import cn.elead.chaos.core.util.ApplicationUtils;
import cn.elead.chaos.framework.properties.Email;
import cn.elead.chaos.framework.properties.ChaosProperties;
import cn.elead.chaos.framework.properties.Generator;
import cn.elead.chaos.framework.properties.Xss;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * <p>
 * Epoms
 * </p>
 * <p>
 * Epoms项目配置工具类
 * </p>
 *
 * @author luopeng
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class ChaosUtil {

	/**
	 * 获取下载地址
	 *
	 * @return
	 */
	public static String getFilePath() {
		ChaosProperties property = getProperties();
		return property.getPath().getFilePath();
	}

	/**
	 * 根据文件名获取下载地址
	 *
	 * @param filename 文件名
	 * @return
	 */
	public static String getDownloadPath(String filename) {
		ChaosProperties property = getProperties();
		return property.getPath().getFilePath() + property.getPath().getPrefix().getDownload() + filename;
	}

	/**
	 * 获取头像上传路径
	 *
	 * @return
	 */
	public static String getAvatarUploadPath() {
		ChaosProperties property = getProperties();
		return property.getPath().getFilePath() + property.getPath().getPrefix().getAvatar();
	}

	/**
	 * 获取头像上传路径
	 *
	 * @return
	 */
	public static String getUploadPath() {
		ChaosProperties property = getProperties();
		return property.getPath().getFilePath() + property.getPath().getPrefix().getUpload();
	}

	/**
	 * 获取上传路径
	 *
	 * @return
	 */
	public static String getUploadResourcePath(String filename) {
		ChaosProperties property = getProperties();
		return property.getPath().getResourcePath() + property.getPath().getPrefix().getUpload() + filename;
	}

	/**
	 * 获取头像上传路径
	 *
	 * @return
	 */
	public static String getResourcePath() {
		ChaosProperties property = getProperties();
		return property.getPath().getResourcePath();
	}

	/**
	 * 获取头像上传路径
	 *
	 * @return
	 */
	public static String getUploadUrl(HttpServletRequest request, String filename) {
		return RequestUtils.getDomain(request) + getUploadResourcePath(filename);
	}

	/**
	 * 获取用户密码最大输入错误次数
	 *
	 * @return
	 */
	public static int getMaxRetryCount() {
		return getProperties().getPwd().getMaxRetryCount();
	}

	/**
	 * 获取生成代码相关配置
	 *
	 * @return
	 */
	public static Generator getGenerator() {
		return getProperties().getGenerator();
	}

	/**
	 * 获取Xss配置
	 *
	 * @return
	 */
	public static Xss getXss() {
		return getProperties().getXss();
	}

	/**
	 * 获取告警Email配置
	 *
	 * @return
	 */
	public static Email getEmail() {
		return getProperties().getEmail();
	}

	/**
	 * 获取CrownProperties
	 *
	 * @return
	 */
	public static ChaosProperties getProperties() {
		return ApplicationUtils.getBean(ChaosProperties.class);
	}

	/**
	 * 获取头像地址
	 * 
	 * @return
	 */
	public static String getAvatarUrl(String avatar) {
		int randomInt = RandomUtil.randomInt(50);
		return getAvatarUrl(avatar, randomInt + "", ".jpg");
	}

	/**
	 * 获取头像地址
	 * 
	 * @return
	 */
	public static String getAvatarUrl(String avatar, String userId) {
		return getAvatarUrl(avatar, userId, ".png");
	}

	/**
	 * 获取头像地址
	 * 
	 * @return
	 */
	public static String getAvatarUrl(String avatar, String userId, String suffix) {
		if (StrUtil.isNotBlank(avatar)) {
			return avatar;
		}
		ChaosProperties property = getProperties();
		// http://localhost:8080/sso/static/avatar/profile/admin.png
		String resourcePrefix = property.getPath().getResourcePrefix();
		String avatarPath = property.getPath().getPrefix().getAvatar();
		return property.getSso().getServer() + resourcePrefix + avatarPath + "profile/" + userId + suffix;
	}

}
